#ifndef _NFC_HAL_TRANSFER_H_
#define _NFC_HAL_TRANSFER_H_

#include <stdint.h>
#include <stdbool.h>
#include "user_config.h"

/* PCD 鍛戒护瀛� */
#define PCD_IDLE					0x00
#define PCD_AUTHENT					0x0E
#define PCD_RECEIVE					0x08
#define PCD_TRANSMIT				0x04
#define PCD_TRANSCEIVE				0x0C
#define PCD_RESETPHASE				0x0F
#define PCD_CALCRC					0x03

/* Registers bits */
#define TX_IEN						BIT(6)
#define RX_IEN						BIT(5)
#define IDEL_IEN					BIT(4)
#define ERR_IEN						BIT(1)
#define TIMER_IEN					BIT(0)

#define TX_IRQ						BIT(6)
#define RX_IRQ						BIT(5)
#define IDEL_IRQ					BIT(4)
#define ERR_IRQ						BIT(1)
#define TIMER_IRQ					BIT(0)

#define COLL_ERR					BIT(3)
#define CRC_ERR						BIT(2)
#define PARITY_ERR					BIT(1)
#define PROTOCOL_ERR				BIT(0)

#define COLLPOS						0x1F

#define RX_ALIGN					(BIT(4)|BIT(5)|BIT(6))
#define TX_LAST_BITS				(BIT(0)|BIT(1)|BIT(2))

/*Hal transceive status code */
#define HAL_STATUS_OK				(0)
#define HAL_STATUS_TIMEOUT			(-1)
#define HAL_STATUS_AUTH_ERR         (-4)
#define HAL_STATUS_CODE_ERR         (-6)
#define HAL_STATUS_NOAUTH_ERR       (-10)
#define HAL_STATUS_BITCOUNT_ERR		(-11)
#define HAL_STATUS_WRITE_ERR        (-15)
#define HAL_STATUS_FRAMINGERR		(-21)
#define HAL_STATUS_UNKNOWN_CMD      (-23)
#define HAL_STATUS_COLLERR			(-24)
#define HAL_STATUS_ACCESSTIMEOUT    (-27)
#define HAL_STATUS_INTEGRITY_ERR	(-35)
#define HAL_STATUS_PARAM_VAL_ERR    (-60)
#define HAL_STATUS_WRITE_REG_ERR	(-61)
#define HAL_STATUS_ERR				(-125)
#define HAL_STATUS_PROTOCOL_ERR		(-126)
#define HAL_STATUS_USER_ERR			(-127)

/* Registers address define */
/* PAGE 0 */
#define REG_COMMAND					0x01
#define REG_COMIEN					0x02
#define REG_DIVIEN					0x03
#define REG_COMIRQ					0x04
#define REG_DIVIRQ					0x05
#define REG_ERROR					0x06
#define REG_STATUS1					0x07
#define REG_STATUS2					0x08
#define REG_FIFODATA				0x09
#define REG_FIFOLEVEL				0x0A
#define REG_WATERLEVEL				0x0B
#define REG_CONTROL					0x0C
#define REG_BITFRAMING				0x0D
#define REG_COLL					0x0E

/* PAGE 1 */
#define REG_MODE					0x11
#define REG_TXMODE					0x12
#define REG_RXMODE					0x13
#define REG_TXCONTROL				0x14
#define REG_TXASK					0x15
#define REG_TXAUTO					0x15
#define REG_TXSEL					0x16
#define REG_RXSEL					0x17
#define REG_RXTHRESHOLD				0x18
#define REG_DEMOD					0x19
#define REG_MFTX					0x1C
#define REG_MFRX					0x1D
#define REG_TYPEB					0x1E
#define REG_SERIALSPEED				0x1F

/* PAGE 2 */
#define REG_CRCRESULTM				0x21
#define REG_CRCRESULTL				0x22
#define REG_MODWIDTH				0x24
#define REG_RFCFG					0x26
#define REG_GSN						0x27
#define REG_CWGSP					0x28
#define REG_MODGSP					0x29
#define REG_TMODE					0x2A
#define REG_TPRESCALER				0x2B
#define REG_TRELOADH				0x2C
#define REG_TRELOADL				0x2D
#define REG_TCOUNTERVALUEH			0x2E
#define REG_TCOUNTERVALUEL			0x2F

/* PAGE 3 */
#define REG_TESTSEL1				0x31
#define REG_TESTSEL2				0x32
#define REG_TESTPINEN				0x33
#define REG_TESTPINVALUE			0x34
#define REG_TESTBUS					0x35
#define REG_AUTOTEST				0x36
#define REG_VERSION					0x37
#define REG_ANALOGTEST				0x38
#define REG_TESTDAC1				0x39
#define REG_TESTDAC2				0x3A
#define REG_TESTADC					0x3B
#define REG_SPECIAL					0x3F


#define HAL_INT_USE_CHECK_REG       0

#if HAL_INT_USE_CHECK_REG
#define INT_PIN (hal_nfc_read_register(0x07) & 0x10)
#else
#define INT_PIN     GPIO_ReadPortPin(READER_IRQ_PORT, READER_IRQ_Pin )//GPIO_ReadInputDataBit(NFC_INT_GPIOX, NFC_INT_GPIO_PIN)
#endif

#define MAX_TRX_BUF_SIZE	        255

#define FSDI 8

typedef struct {
    void (*hw_init)(void);
    int (*hw_transceive)(uint8_t *, int, uint8_t *, int *);

    void (*hw_pin_init)(void);
    void (*hw_set_pcd_power)(uint8_t);

    void (*hw_clock_init)(void);
    void (*hw_msleep)(uint32_t);
    uint32_t (*hw_get_tick)(void);
    int (*hw_is_timeout)(uint32_t, uint32_t);

    void (*hw_put_char)(uint8_t);
} hal_driver_interface_t;

typedef struct {
    int8_t mf_command;
    uint16_t mf_length;
    uint8_t mf_data[MAX_TRX_BUF_SIZE];
    uint8_t type;
    uint8_t scene;
} hal_nfc_transceive_t;

typedef struct pcd_info_s
{
    unsigned char     uc_nid;
    unsigned char     uc_nad_en;
    unsigned char     uc_cid;
    unsigned char     uc_cid_en;
    unsigned int      ui_fsc;
    unsigned int      ui_fwi;
    unsigned int      ui_sfgi;
    unsigned char     uc_pcd_pcb;
    unsigned char     uc_picc_pcb;
    unsigned int      uc_pcd_txr_num;
    unsigned int      uc_pcd_txr_lastbits;
    unsigned char	  uc_wtxm;
} pcd_info_s;

typedef enum {

    POS=1,
    EMV=0


} Scene;


extern pcd_info_s g_pcd_module_info;
extern const hal_driver_interface_t drv_interface;


void hal_nfc_init(void);
int hal_nfc_write_register(uint8_t addr, uint8_t value);
uint8_t hal_nfc_read_register(uint8_t addr);
void hal_nfc_set_register_bit(uint8_t addr, uint8_t mask);
void hal_nfc_clear_register_bit(uint8_t addr, uint8_t mask);

int pcd_com_transceive(hal_nfc_transceive_t *pi);


void pcd_default_info(void);

#endif
